package com.xingkeduo.configuration;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class WechatFilter implements HandlerInterceptor {
	Logger log = LoggerFactory.getLogger(WechatFilter.class);

	public final static ThreadLocal<Long> operateIdThreadLocal = new ThreadLocal<>();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		String requestURI = request.getRequestURI();
		String queryString = request.getQueryString();
		log.info("===============================REQUEST-START");
		log.info("开始验证 url:{},method:{},操作人:{}", requestURI,request.getMethod(), request.getHeader("operator"));
		if (StringUtils.isNotBlank(queryString)) {
			log.info("================================GET-REQUEST PATH:{}", queryString);
		}
		try {
			if (request instanceof RequestWrapper) {
				RequestWrapper requestWrapper = (RequestWrapper) request;
	            String jsonBody = requestWrapper.getBody();
				if(StringUtils.isNotBlank(jsonBody)) {
				log.info("================================POST-REQUEST PARAM: {}", jsonBody);
				}
			}
		} catch (Exception e) {
			log.info("获取请求参数异常,e:{}", e);
		}
		try {
			operateIdThreadLocal.set(Long.parseLong(request.getHeader("operator")));
		} catch (NumberFormatException e) {
			operateIdThreadLocal.set(0L);
		}
		log.info("================================REQEUST-END");
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object o, Exception e) throws Exception {
		operateIdThreadLocal.remove();
//		log.info("RSEPONSE-START===============================");
//		log.info("开始验证 url:{},操作人:{}", httpServletRequest.getRequestURI(), httpServletRequest.getHeader("operator"));
//		try {
//			if (httpServletResponse instanceof ResponseWrapper) {
//				ResponseWrapper responseWrapper = (ResponseWrapper) httpServletResponse;
//				String responseString = responseWrapper.getResponseData(httpServletResponse.getCharacterEncoding());
//				if (StringUtils.isNotBlank(responseString)) {
//					log.info("RESPONSE-DATA:{}", responseString);
//				}
//				ServletOutputStream outputStream = httpServletResponse.getOutputStream();
//		        outputStream.write(responseString.getBytes());
//		        outputStream.flush();
//		        outputStream.close();
//				//httpServletResponse.getOutputStream().flush();
//			}
//		} catch (Exception ex) {
//			log.error("获取输出异常，e:{}", ex);
//		}
//		log.info("RSEPONSE-END=================================");
	}

}
